import json
import urllib.parse

class BaiduPanRequest:
    __common_params_map = {
        "shareid": "7058460988",
        "sekey": "QOpso0iHFupk3So5q6AI4R5De5K6Cs0vpky6dNUIMIM%3D",
        "uk": "1987892629",
        "channel": "chunlei",
        "web": "1",
        "app_id": "250528",
        "bdstoken": "9213743cb507e5673caddd6ab2fb05c8",
        "logid": "OTIyN0I5RjY3NjNFNjJDNDIwNzkyODg0RjBGNzQ4NUE6Rkc9MQ==",
        "clienttype": "0",
        "dp-logid": "23453500175086050068",
        "t": "0.019088016650529394"
    }

    def __init__(self, initFilePath):
        # 读取init.ini文件，获取第一行作为commonParams,第二行作为cookies
        with open(initFilePath) as f:
            lines = f.readlines()
            # 解析查询字符串为字典
            parsed_dict = urllib.parse.parse_qs(lines[0].strip())
            # 处理重复键，只保留第一个值
            processed_dict = {key: value[0] for key, value in parsed_dict.items()}
            commonParams = processed_dict

            referer = lines[1].strip()
            self.root_dir = lines[2].strip()
            self.root_dir_push = lines[3].strip()
            cookies = lines[4].strip()

            self.headers = {
                'Accept': 'application/json, text/javascript, */*; q=0.01',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                'Connection': 'keep-alive',
                'Cookie': cookies,  # 请替换为实际的Cookie值
                'Referer': referer,
                'Sec-Fetch-Dest': 'empty',
                'Sec-Fetch-Mode': 'cors',
                'Sec-Fetch-Site': 'same-origin',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
                'X-Requested-With': 'XMLHttpRequest',
                'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
                'sec-ch-ua-mobile': '?0',
                'sec-ch-ua-platform': '"macOS"'
            }
            self.common_params = {}
            # 循环遍历commonParams字典，将self.__common_params_map中存在的键值对添加到self.common_params中
            for key, value in commonParams.items():
                if key in self.__common_params_map:
                    self.common_params[key] = value

    def getHeaders(self,  headers=None):
        if not headers:
            headers = self.headers
        else:
            headers.update(self.headers)
        return headers

    def getParams(self,  params=None):
        if not params:
            params = self.common_params
        else:
            params.update(self.common_params)
        return params